<template>
  <el-form class="quick-filter-add-form form-margin--mini"
           :model="form"
           :rules="rules"
           label-width="80px"
           label-position="top"
           ref="form">
    <el-form-item :label="$t('custom.stcdm')"
                  class="quick-filter-add-form--text"></el-form-item>
    <el-form-item prop="name">
      <el-input v-model="form.name"
                :placeholder="$t('custom.petqfn')"></el-input>
    </el-form-item>
    <el-form-item class="dialog-footer no-padding">
      <ThrottleButton type="primary"
                      ref="throttle-button"
                      @event:throttle-click="handleSubmit"
                      size="mini">{{$t('save')}}</ThrottleButton>
    </el-form-item>
  </el-form>
</template>

<script>
import ThrottleButton from '@/components/throttle-button'
export default {
  name: 'QuickFilterAddForm',
  components: {
    ThrottleButton
  },
  props: {
    filter: {
      type: Object,
      default: () => ({})
    },
    order: {
      type: Object,
      default: () => ({})
    },
    integratin: {
      type: Array,
      default: () => ([])
    },
    type: {
      type: String,
      require: true
    }
  },
  data () {
    return {
      form: {

      },
      rules: {
        name: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur', pattern: '[^ \x22]+' }
        ]
      }
    }
  },
  methods: {

    async handleSubmit (done) {
      try {
        const value = await this.$refs.form.validate()
        if (value) {
          const { filter, integratin, type, form, order } = this
          // const { id } = this.$route.query
          const { disposeQuickFilter } = this.$interface.reports
          const { get } = this._
          const params = {
            type,
            name: get(form, 'name'),
            filterJson: {
              filter,
              order: [order],
              integratin
            }
          }

          // if (id && id.indexOf('-') === -1) {
          //   set(params, 'id', id)
          // }

          const { data: response } = await disposeQuickFilter(params)
          if (response.code !== 0) {
            throw get(response, 'msg', '')
          }

          this.$message.success(this.$t('success'))
          this.$emit('success')
          this.handleCancel()
        }
      } catch (error) {
        if (typeof error === 'string') {
          this.$message.error(error)
        }
      } finally {
        if (typeof done === 'function') {
          done()
        }
      }
    },

    handleCancel () {
      this.resetFields()
      document.body.click()
    },
    resetFields () {
      this.$refs.form.resetFields()
    }

  }
}
</script>

<style lang="scss" scoped>
.quick-filter-add-form {
  &--text {
    ::v-deep .el-form-item__label {
      font-size: 14px;
      color: #404040;
      line-height: 18px;
    }
  }
}
</style>
